package com.huaboot.system.controller;
import com.huaboot.commont.constant.Constant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.huaboot.commont.core.BaseResponse;
import com.huaboot.commont.core.ResultUtils;
import com.huaboot.system.convert.SysOrgConvert;
import com.huaboot.system.entity.SysOrgEntity;
import com.huaboot.system.service.SysOrgService;
import com.huaboot.system.query.SysOrgQuery;
import com.huaboot.system.vo.SysOrgVO;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.huaboot.commont.page.PageResult;
import java.util.List;

/**
* 机构管理
*
* @author ff xxx
* @since 1.0.0 2023-09-03
*/
@RestController
@RequestMapping("sys/org")
@Tag(name="机构管理")
@AllArgsConstructor
public class SysOrgController {
    private final SysOrgService sysOrgService;


    @GetMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('sys:org:list')")
    public BaseResponse<List<SysOrgVO>> list() {
        List<SysOrgVO> list = sysOrgService.getList();

        return ResultUtils.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('system:org:info')")
    public BaseResponse<SysOrgVO> get(@PathVariable("id") Long id){
        SysOrgEntity entity = sysOrgService.getById(id);
        SysOrgVO vo = SysOrgConvert.INSTANCE.convert(entity);
        // 获取上级机构名称
        if (Constant.ROOT != (entity.getPid())) {
            SysOrgEntity parentEntity = sysOrgService.getById(entity.getPid());
            vo.setParentName(parentEntity.getName());
        }

        return ResultUtils.success(vo);
    }



    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:org:save')")
    public BaseResponse<?> save(@RequestBody SysOrgVO vo){
        sysOrgService.save(vo);

        return ResultUtils.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('system:org:update')")
    public BaseResponse<?> update(@RequestBody  SysOrgVO vo){
        sysOrgService.update(vo);

        return ResultUtils.success("修改成功");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:org:delete')")
    public BaseResponse<?> delete(@PathVariable("id") Long id){
        sysOrgService.delete(id);

        return ResultUtils.success("删除成功");
    }
}
